<?php

/***

LZRoster: A roster for World of Warcraft.
Copyright (C) 2007  Emmanuel Cron

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

***/

require_once dirname(__FILE__)."/Time.class.php";
require_once dirname(__FILE__)."/Races.class.php";
require_once dirname(__FILE__)."/Classes.class.php";
require_once dirname(__FILE__)."/Config.class.php";
require_once dirname(__FILE__)."/Tooltip.class.php";
require_once dirname(__FILE__)."/TradeskillContainer.class.php";
require_once dirname(__FILE__)."/TradeskillSpecializationContainer.class.php";
require_once dirname(__FILE__)."/Localization.class.php";

class CharacterDisplayer
{	
	static private $instance;
	
	private $path_to_races = "characters/mini/races";
	private $path_to_classes = "characters/mini/classes";
	
	private $path_to_img_remove = "preferences/remove.png";

	private function __construct(){}
	
	static function GetInstance()
	{
		if ( empty(self::$instance) )
		{
			self::$instance = new CharacterDisplayer();
		}
		
		return self::$instance;
	}
	
	function Name($character)
	{
		$string = null;
		$url = null;
		
		$u8_name = utf8_encode($character->GetName());
		
		$url .= Config::GetInstance()->GetWoWArmory()."character-sheet.xml?r=";
		$url .= utf8_encode(Config::GetInstance()->GetRosterServer());
		$url .= "&amp;n=".urlencode($u8_name);
		
		$string .= "<a href='".$url."'>";
		$string .= "<img src='img/".$this->path_to_races."/".$character->GetRace()."_".$character->GetSexId().".jpg' alt='' style='float: left; padding-right: 5px; vertical-align: middle;' ";
		$string .= "onmouseover=\"return overlib('<img src=\\'img/".$this->path_to_classes."/".$character->GetClass().".png\\' alt=\\'\\' style=\\'height: 16px; vertical-align: middle;\\'/> <em>".Tooltip::GetInstance()->Flatten(Classes::GetInstance()->GetLiteral( $character->GetClass() ))."</em>', ";
		$string .= "CAPTION, '".Tooltip::GetInstance()->Flatten($u8_name).", ".Tooltip::GetInstance()->Flatten(Races::GetInstance()->GetLiteral( $character->GetRace() ))."');\" onmouseout=\"return nd();\" />";
		$string .= "</a> ";
		$string .= "<a onmouseover=\"return overlib('".Tooltip::GetInstance()->Flatten(Localization::GetInstance()->Get('display this character on the armory'))."');\" onmouseout=\"return nd();\" href='".$url."'>";
		$string .= $u8_name;
		$string .= "</a>";
		$string .= "<br />";
		$string .= Localization::GetInstance()->Get('level');
		$string .= " ";
		$string .= $character->GetLevel();
		
		return $string;
	}
	
	function Class_($character)
	{
		$string = null;
		
		$string .= "<img src='img/".$this->path_to_classes."/".$character->GetClass().".png' alt='' style='vertical-align: middle;' onmouseover=\"return overlib('<em>".Classes::GetInstance()->GetLiteral( $character->GetClass() )."</em>');\" onmouseout=\"return nd();\" />";
		
		return $string;
	}
	
	function GuildRank($character)
	{
		return $character->GetGuildRank();
	}
	
	function GuildTitle($character)
	{
		return $character->GetGuildTitle();
	}
	
	function Links($character)
	{
		$string = null;
		
		$string .= "<a onmouseover=\"return overlib('".Tooltip::GetInstance()->Flatten(Localization::GetInstance()->Get('click on this icon to display the related data'))."', CAPTION, '".Tooltip::GetInstance()->Flatten(Localization::GetInstance()->Get('reputations'))."');\" onmouseout=\"return nd();\" href='".Config::GetInstance()->GetWoWArmory()."character-reputation.xml?r=".utf8_encode(Config::GetInstance()->GetRosterServer())."&amp;n=".urlencode(utf8_encode($character->GetName()))."'>";
		$string .= "<img src='img/reputation.gif' alt='' />";
		$string .= "</a>";
		$string .= "<a onmouseover=\"return overlib('".Tooltip::GetInstance()->Flatten(Localization::GetInstance()->Get('click on this icon to display the related data'))."', CAPTION, '".Tooltip::GetInstance()->Flatten(Localization::GetInstance()->Get('skills'))."');\" onmouseout=\"return nd();\" href='".Config::GetInstance()->GetWoWArmory()."character-skills.xml?r=".utf8_encode(Config::GetInstance()->GetRosterServer())."&amp;n=".urlencode(utf8_encode($character->GetName()))."'>";
		$string .= "<img src='img/skills.gif' alt='' />";
		$string .= "</a>";
		$string .= "<a onmouseover=\"return overlib('".Tooltip::GetInstance()->Flatten(Localization::GetInstance()->Get('click on this icon to display the related data'))."', CAPTION, '".Tooltip::GetInstance()->Flatten(Localization::GetInstance()->Get('talents'))."');\" onmouseout=\"return nd();\" href='".Config::GetInstance()->GetWoWArmory()."character-talents.xml?r=".utf8_encode(Config::GetInstance()->GetRosterServer())."&amp;n=".urlencode(utf8_encode($character->GetName()))."'>";
		$string .= "<img src='img/talents.gif' alt='' />";
		$string .= "</a>";
		$string .= "<a onmouseover=\"return overlib('".Tooltip::GetInstance()->Flatten(Localization::GetInstance()->Get('click on this icon to display the related data'))."', CAPTION, '".Tooltip::GetInstance()->Flatten(Localization::GetInstance()->Get('arena teams'))."');\" onmouseout=\"return nd();\" href='".Config::GetInstance()->GetWoWArmory()."character-arenateams.xml?r=".utf8_encode(Config::GetInstance()->GetRosterServer())."&amp;n=".urlencode(utf8_encode($character->GetName()))."'>";
		$string .= "<img src='img/arena.gif' alt='' />";
		$string .= "</a>";
		
		return $string;
	}
	
	function Level($character)
	{
		return $character->GetLevel();
	}
	
	function Specializations($character)
	{
		return $this->SpecializationDisplay($character);
	}
	
	private function SpecializationDisplay($character, $delete=false)
	{
		$string = null;
		
		foreach ($character->GetSpecializations() as $id) {
			$specialization = TradeskillSpecializationContainer::GetInstance()->GetTradeskillSpecialization($id);
			
			if ($specialization != null) {
				if ($delete == true) {
					$string .= "<a onclick=\"return confirm('".Tooltip::GetInstance()->Flatten(Localization::GetInstance()->Get('removing a specialization from a character is only useful (...)'))."');\"";
					$string .= "onmouseover=\"return overlib('".Tooltip::GetInstance()->Flatten(Localization::GetInstance()->Get('remove %s for %t', $specialization->GetName(), $character->GetName()))."');\"";
					$string .= "onmouseout=\"return nd();\"";
					$string .= "href='preferences.php?s_del&id=".$character->GetId()."&s=".$specialization->GetId()."'>";
				}
				
				$string .= "<img src='img/".$specialization->GetIcon()."' alt='' ";
				$string .= "onmouseover=\"return overlib('".Tooltip::GetInstance()->Flatten($specialization->GetName())."')\" onmouseout=\"return nd();\"";
				$string .= ">";
				
				if ($delete == true) {
					$string .= "</a>";
				}
			} else {
				$string .= "<img src='img/trades_specializations/unknown.png' alt='' ";
				$string .= "onmouseover=\"return overlib('".Tooltip::GetInstance()->Flatten(Localization::GetInstance()->Get('unknown specialization'))."')\" onmouseout=\"return nd();\"";
				$string .= ">";
			}
		}
		
		if ($string == null)
		{
			$string .= "-";
		}
		
		return $string;
	}
	
	function __DeleteSpecialization($character)
	{
		return $this->SpecializationDisplay($character, true);
	}
	
	function Experience($character)
	{
		$string = null;
		
		$o_body = "<em>".( $character->GetRestedExperience() != null ? Tooltip::GetInstance()->Flatten(Localization::GetInstance()->Get('rested')).$character->GetRestedExperience() : Tooltip::GetInstance()->Flatten(Localization::GetInstance()->Get('character is not rested')))."</em>";
		$o_title = Tooltip::GetInstance()->Flatten(Localization::GetInstance()->Get('experience')).": ".$character->GetCurrentExperience()."/".$character->GetMaxExperience();
		
		$xp_percentage = intval( 100 * $character->GetCurrentExperience() / $character->GetMaxExperience() );
		
		$string .= "<img src='img/characters/xpbarempty.gif' alt='' ";
		$string .= "onmouseover=\"return overlib('".$o_body."', CAPTION, '".$o_title."')\" ";
		$string .= "onmouseout=\"return nd();\" style='height: 15px; margin-left: 4px; position: absolute; width: 100px;' />";
		$string .= "<img src='img/characters/xpbar.gif' alt='' ";
		$string .= "onmouseover=\"return overlib('".$o_body."', CAPTION, '".$o_title."')\" ";
		$string .= "onmouseout=\"return nd();\" style='height: 14px; margin-left: 4px; position: absolute; width: ".$xp_percentage."px;' />";
		
		$string .= "<span ";
		$string .= "onmouseover=\"return overlib('".$o_body."', CAPTION, '".$o_title."')\" ";
		$string .= "onmouseout=\"return nd();\" style='color: #eeeeee; font-weight: bold; margin-left: 44px; position: relative;'>";
		$string .= $xp_percentage."%";
		$string .= "</span>";
		
		return $string;
	}
	
	function Revision($character)
	{
		return $character->GetRevision();
	}
	
	function LastUpdate($character)
	{
		$string = null;
		
		$string .= "<strong>";
		$string .= date("d/m/Y, H:i:s", $character->GetLastUpdate());
		$string .= "</strong>";
		$string .= "<br />";
		$string .= "<em>";
		$string .= Time::GetInstance()->GetLiteralDelayByTimestamp($character->GetLastUpdate());
		$string .= "</em>";
		
		return $string;
	}
	
	function Tradeskills($character)
	{
		$string = null;
		
		$temporary_trades = array();
		foreach ($character->GetProfessions() as $tradeskill_locale)
		{
			$tradeskill = TradeskillContainer::GetInstance()->GetTradeskillByLocale($tradeskill_locale);
			
			if (!$tradeskill->IsHidden()) {
				array_push($temporary_trades, $tradeskill->GetId());
			}
		}
		
		foreach (TradeskillContainer::GetInstance()->GetAllTradeskills() as $tradeskill)
		{
			if ( in_array($tradeskill->GetId(), $temporary_trades) )
			{
				$string .= "<a href='tradeskills.php?t=".$tradeskill->GetId()."'>";
				$string .= "<img src='img/".$tradeskill->GetIcon()."' alt='' style='vertical-align: middle;' ";
				$string .= "onmouseover=\"return overlib('".Tooltip::GetInstance()->Flatten(Localization::GetInstance()->Get('click on this icon to display the related data'))."', CAPTION, '".Tooltip::GetInstance()->Flatten($tradeskill->GetNameLiteral())."');\" ";
				$string .= "onmouseout=\"return nd();\" />";
				$string .= "</a>";
			}
		}
		
		return $string;
	}
	
	function TimePlayed($character)
	{
		$string = null;
		
		$string .= "<strong>";
		$string .= Time::GetInstance()->ConvertSecondsToTime($character->GetTimePlayed());
		$string .= "</strong>";
		$string .= "<br />";
		$string .= Localization::GetInstance()->Get('L(EVEL)').$character->GetLevel().": ";
		$string .= "<em>";
		$string .= Time::GetInstance()->ConvertSecondsToTime($character->GetTimeLevelPlayed());
		$string .= "</em>";
		
		return $string;
	}
	
	function __Delete($character)
	{
		$string = null;
		
		$string .= "<a href='preferences.php?c_del&amp;id=".$character->GetId()."' ";
		$string .= "onclick=\"return confirm('".Tooltip::GetInstance()->Flatten( Localization::GetInstance()->Get('do you really want to delete this character from the roster? warning: the character will (...)') )."');\" ";
		$string .= "onmouseover=\"return overlib('".Tooltip::GetInstance()->Flatten( Localization::GetInstance()->Get('delete this character from the roster') )."');\" onmouseout=\"return nd();\">";
		$string .= "<img src='img/".$this->path_to_img_remove."' alt='' /></a>";
		
		return $string;
	}
	
	function __AdminDelete($character)
	{
		$string = null;
		
		$string .= "<a href='admin.php?c_del&amp;id=".$character->GetId()."' ";
		$string .= "onclick=\"return confirm('".Tooltip::GetInstance()->Flatten( Localization::GetInstance()->Get('do you really want to delete this character from the roster?') )."');\" ";
		$string .= "onmouseover=\"return overlib('".Tooltip::GetInstance()->Flatten( Localization::GetInstance()->Get('delete this character from the roster') )."');\" onmouseout=\"return nd();\">";
		$string .= "<img src='img/".$this->path_to_img_remove."' alt='' /></a>";
		
		return $string;
	}
	
	function __DeleteTradeskill($character)
	{
		$string = null;
		
		if ( count($character->GetProfessions()) < 1 )
		{
			$string .= "<em>".Localization::GetInstance()->Get('no tradeskill to display')."</em>";
		}
		else
		{
			$string .= Localization::GetInstance()->Get('remove: ');
			
			foreach ($character->GetProfessions() as $tradeskill_locale)
			{
				$tradeskill = TradeskillContainer::GetInstance()->GetTradeskillByLocale($tradeskill_locale);
				
				$string .= "<img src='img/".$tradeskill->GetIcon()."' alt='' style='vertical-align: middle;' />";
				$string .= "<a href='preferences.php?t_del&amp;id=".$character->GetId()."&amp;t=".$tradeskill->GetId()."' ";
				$string .= "onmouseover=\"return overlib('".Tooltip::GetInstance()->Flatten(Localization::GetInstance()->Get('remove %s for %t', $tradeskill->GetNameLiteral(), $character->GetName()))."');\" ";
				$string .= "onclick=\"return confirm('".Tooltip::GetInstance()->Flatten(Localization::GetInstance()->Get('removing a tradeskill from a character is only useful (...)'))."')\" ";
				$string .= "onmouseout=\"return nd();\">";
				$string .= $tradeskill->GetNameLiteral();
				$string .= "</a>";
				$string .= " ";
			}
		}
		
		return $string;
	}
}

?>